package org.example.lambda;

import org.example.entity.Person;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author why
 * @date 2022/11/1220:36
 */
public class Unit1ExerciseJava7 {


    public static void main(String[] args) {

        List<Person> personList = Arrays.asList(
                new Person("Cheng","fen",23),
                new Person("Cheng","fen",23),
                new Person("Xiao","Can",23),
                new Person("Chang","yang",23),
                new Person("Clin","Yang",23),
                new Person("Lin","Cang",23)
                );

        // Step 1 : Sort list by last name
        Collections.sort(personList,new Comparator<Person>(){
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getLastName().compareTo(o2.getLastName());
            }
        });
        // Step 2: Create a method that print all element in the list
        System.out.println("printAllElement");
        printAllElement(personList);


        // Step 3: Create a method that print all element that have last name beginning with C

        System.out.println("printAllElementLastNameBeginningWithC");
        printAllElementLastNameBeginningWithC(personList);


        // Step 4: Create a method that print all element that have first name beginning with C
        System.out.println("printAllElementFirstNameBeginningWithC");
        printAllElementBeginningWithCondition(personList, new Condition() {
            @Override
            public boolean test(Person person) {
                return person.getFirstName().startsWith("C");
            }
        });



    }

    private static void printAllElementLastNameBeginningWithC(List<Person> personList) {
        for(Person person:personList){
            if (person.getLastName().startsWith("C")){
                System.out.println(person);
            }
        }

    }
    private static void printAllElementBeginningWithCondition(List<Person> personList,Condition condition) {
        for(Person person:personList){
            if (condition.test(person)){
                System.out.println(person);
            }
        }

    }

    private static void printAllElement(List<Person> personList) {
        for(Person person:personList){
            System.out.println(person);
        }
    }





}


